package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.common.util.DateUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevCollection;
import cn.sciento.farm.devicemanager.dao.entity.DevCollectionType;
import cn.sciento.farm.devicemanager.dao.entity.DevController;
import cn.sciento.farm.devicemanager.dao.entity.DevDevice;
import cn.sciento.farm.devicemanager.dao.mapper.CollectionHistoryMapper;
import cn.sciento.farm.devicemanager.dao.mapper.CollectionMapper;
import cn.sciento.farm.devicemanager.dao.model.CollectionInfo;
import cn.sciento.farm.devicemanager.dao.model.CollectionNode;
import cn.sciento.farm.devicemanager.dao.model.HistoryGetData;
import cn.sciento.farm.devicemanager.dao.model.HistoryGetForm;
import cn.sciento.farm.devicemanager.service.CollectionService;
import cn.sciento.farm.devicemanager.service.CollectionTypeService;
import cn.sciento.farm.devicemanager.service.DeviceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionMapper collectionMapper;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private CollectionTypeService collectionTypeService;


    @Autowired
    private CollectionHistoryMapper collectionHistoryMapper;

    @Override
    public int updateOnline(String eui, String node, String circuit, int up) {
        QueryWrapper queryWrapper = new QueryWrapper<DevCollection>();
        queryWrapper.eq("eui", eui);
        if(StringUtils.isNotEmpty(node)){
            queryWrapper.eq("node", node);
        }
        if(StringUtils.isNotEmpty(circuit)){
            queryWrapper.eq("circuit", circuit);
        }
        DevCollection devCollection = new DevCollection();
        devCollection.setOnline(up);
        int count = collectionMapper.update(devCollection, queryWrapper);
        return count;
    }

    @Override
    public DevCollection getByArg(String eui, String node, String circuit) {
        QueryWrapper<DevCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("eui", eui);
        queryWrapper.eq("node", node);
        queryWrapper.eq("circuit", circuit);
        return collectionMapper.selectOne(queryWrapper);
    }

    @Override
    public DevCollection getByDeepSoilArg(String eui, String node, String name) {
        QueryWrapper<DevCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("eui", eui);
        queryWrapper.eq("node", node);
        queryWrapper.eq("name", name);
        return collectionMapper.selectOne(queryWrapper);
    }

    @Override
    public int updateData(Long id, float realData, Long time) {
        DevCollection devCollection = new DevCollection();
        devCollection.setLastData(realData);
        devCollection.setLastTime(time);
        devCollection.setId(id);
        return collectionMapper.updateById(devCollection);
    }

    @Override
    public int insert(DevCollection deviceCollection) {
        int count = collectionMapper.insert(deviceCollection);
        return count;
    }

    @Override
    public int updateById(DevCollection devCollection) {
        return collectionMapper.updateById(devCollection);
    }

    @Override
    public DevCollection getById(long id) {
        return collectionMapper.selectById(id);
    }

    @Override
    public int deleteById(long id) {
        return collectionMapper.deleteById(id);
    }

    @Override
    public List<DevCollection> getByEui(String eui) {
        QueryWrapper queryWrapper = new QueryWrapper<DevCollection>();
        queryWrapper.eq("eui", eui);
        List<DevCollection> devCollectionList = collectionMapper.selectList(queryWrapper);
        return devCollectionList;
    }

    @Override
    public List<DevCollection> getByLandId(int landId) {
        List<DevDevice> devDeviceList = deviceService.getByLandId(landId);
        List<String> idList = devDeviceList.stream().map(item -> item.getId()).collect(Collectors.toList());
        List<DevCollection> devCollectionList = new ArrayList<>();
        if (idList.size() > 0) {
            QueryWrapper queryWrapper = new QueryWrapper<DevCollection>();
            queryWrapper.in("eui", idList);
            devCollectionList = collectionMapper.selectList(queryWrapper);
        }
        return devCollectionList;
    }

    @Override
    public List<CollectionNode> getHistoryById(Long collectionId, Long beginTime, Long endTime) {
        List<CollectionNode> dataNodes = collectionHistoryMapper.getBetweenHistory(collectionId, beginTime, endTime);
        return dataNodes;
    }

    @Override
    public List<HistoryGetData> getHistory(HistoryGetForm historyGetForm) {
        return collectionHistoryMapper.getHistoryList(historyGetForm);
    }

    @Override
    public List<HistoryGetData> getMonthHistory(HistoryGetForm historyGetForm) {
        return collectionHistoryMapper.getMonthHistory(historyGetForm);
    }

    @Override
    public List<CollectionInfo> getCollectionInfo(String eui) {
        List<DevCollection> devCollectionList = getByEui(eui);
        List<DevCollectionType> collectionTypeList = collectionTypeService.list();
        List<CollectionInfo> collectionInfoList = new ArrayList<>();
        for (DevCollection collection : devCollectionList) {
            CollectionInfo collectionInfo = new CollectionInfo();
            BeanUtils.copyProperties(collection, collectionInfo);
            DevCollectionType collectionType = getCollectionType(collection.getType(), collectionTypeList);
            if (collectionType == null) {
                continue;
            }
            collectionInfo.setCollectionType(collectionType);
            collectionInfoList.add(collectionInfo);
        }
        return collectionInfoList;
    }

    @Override
    public List<DevCollection> getByAreaId(int areaId) {
        List<DevDevice> devDeviceList = deviceService.getByAreaId(areaId);
        List<String> idList = devDeviceList.stream().map(item -> item.getId()).collect(Collectors.toList());
        List<DevCollection> devCollectionList = new ArrayList<>();
        if(idList.size() >0){
            QueryWrapper queryWrapper = new QueryWrapper<DevCollection>();
            queryWrapper.in("eui", idList);
            devCollectionList = collectionMapper.selectList(queryWrapper);
        }
        return devCollectionList;
    }

    @Override
    public int updateOnlineByCheck(int durationHour) {

        return collectionMapper.updateOnlineByCheck(System.currentTimeMillis() - durationHour * 3600*1000);
    }

    @Override
    public List<DevCollection> getByTenantId(String tenantId) {
        QueryWrapper queryWrapper = new QueryWrapper<DevCollection>();
        queryWrapper.eq("tenant_id",tenantId);
        return collectionMapper.selectList(queryWrapper);
    }

    private DevCollectionType getCollectionType(String type, List<DevCollectionType> collectionTypeList) {
        for (int i = 0; i < collectionTypeList.size(); i++) {
            if (collectionTypeList.get(i).getId().equals(type)) {
                return collectionTypeList.get(i);
            }
        }
        return null;
    }
}
